<?php
/**
 * BNGAPIService.php
 * description:
 * Created by www.
 * User: SCF
 * Date: 2018/9/19 11:21
 *
 */

namespace App\Services\ThirdAPI;

use App\Models\Game;
use App\Models\Partner;
use common\enum\CommonEnum;
use common\utils\RedisKeys;
use Illuminate\Support\Facades\Redis;
use common\utils\SysCode;


class BNGAPIService
{
    protected $apiName="bng";
    protected $api ;
    protected $apiToken ;
    protected $extra;
    protected $header = ["Accept" => "application/json"];


    public function getGameNames()
    {
        $data = Game::where(['partner_name' => $this->apiName])->get()->toArray();
        $needUpdate = Redis::set(RedisKeys::BNG_LOCK_GAMES, date("Y-m-d H:i:s"), 'nx', 'ex', 3600);
        if (!$data || $needUpdate) {
            $data = $this->gamelist();
        }
        $games = [];
        foreach ($data as $d) {
            $games[$d['game_code']] = $d['name'];
        }
        return $games;
    }


    //获取游戏列表
    public function gamelist(){
        $data = [
            'api_name' => $this->apiName
        ];
        $url = config('app.rpcserver') . "/api/getGameList";
        $res = GameApiService::request($url,'get',$data);
        //$res = json_decode ($res,true);
        // 获取当前所有数据，用以排除已有的数据
        $currentData = Game::where(['partner_name' => $this->apiName])->get()->toArray();
        $formatData = [];
        foreach ($currentData as $value) {
            $formatData[$value['game_code']] = $value;
        }
        $insertData = [];
        foreach ($res as $value) {
            if (empty($formatData[$value['game_id']])) {
                $insertData[] = [
                    'partner_id' => $value['partner_id'],
                    'partner_name' => $value['partner_name'],
                    'name' => $value['name'],
                    'extend' => $value['extend'],
                    'img_url' => $value['img_url'],
                    'game_code' => $value['game_code'],
                    'game_url' => $value['game_url'],
                    'type' => $value['type'],
                    'game_type' => $value['game_type'], // 电子游戏
                ];
            }
        }
        $currentData = array_merge($currentData, $insertData);
        if (count($insertData) > 0) {
            GameApiService::saveGame($currentData);
            if (!$res) {
                Redis::del(RedisKeys::BNG_LOCK_GAMES);
                $msg="BNG BNGAPIService::gamelist 保存游戏列表报错失败：" . json_encode($res->getErrors(), JSON_UNESCAPED_UNICODE);
                BaseHandler::error($msg);
                return error_exit(SysCode::INTERNAL_SERVER_ERROR, $msg);
            }
        }
        return $currentData;
    }
    /**
     * 代理商获取玩家游戏下注数据或总社获取玩家游戏下注日志   OK
     * @param $startTime
     * @param $endTime
     * @return mixed
     * @throws \Exception
     */
    function getOrders($startTime, $endTime){

        $data = [
            'api_name' => $this->apiName,
            'start_time' => date('Y-m-d H:i:s',strtotime($startTime)+4*3600),
            'end_time' => date('Y-m-d H:i:s',strtotime($endTime)+4*3600)
        ];
        $url = config('app.rpcserver') . "/api/getOrders";
        $res = GameApiService::request($url,'get',$data);
        amount_format_array($res,['bet_money','prize_money','winloss_money','contribute_money','bonus_money','valid_money'],true);
        return $res;
    }
}